import {
  uploadToQiniu
} from "../../utils/uploadToQiniu"

Page({

  data: {
    content: '',
    image: [],
    do: '',
    id: '',
    orderDetail: null,
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id,
        ordertype: options.ordertype,
      })
    }
    if (options.ordertype == 0) {
      this.setData({
        do: 'JfOrderDetail'
      })
    } else if (options.ordertype == 1) {
      this.setData({
        do: 'JfOrderDetail2'
      })
    }
    this.getList()
  },

  getList() {
    return new Promise((reslove, reject) => {
      getApp().globalData.post({
        data: {
          do: this.data.do,
          id: this.data.id
        }
      }).then(res => {
        let list = res.data.result
        this.setData({
          orderDetail: list,
        })
      })
    })
  },

  change_content(e) {
    let content = e.detail.value
    this.setData({
      content
    })
  },

  change_image(e) {
    let upload_url = e.detail.current;
    let arr = this.data.image || [];
    upload_url.forEach((v, i) => {
      uploadToQiniu(v).then(s => {
        arr.push(s)
        this.setData({
          image: arr
        })
      });
    })
  },

  remove_image(e) {
    let arr = this.data.image;
    arr.splice(e.detail.index, 1);
    this.setData({
      image: arr
    })
  },


  forumannounce() {
    if (!this.data.content && this.data.image.length == 0) {
      getApp().showToast('内容或图片不能为空')
      return
    }
    getApp().globalData.posts('/api/app/residential/sorting/ouyapi/forumannounce.html', {
      content: this.data.content,
      img: this.data.image.join(','),
      genre: 2,
      jfrecord_id: this.data.id,
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        if (res.data.code == 1) {
          wx.navigateBack()
        }
      })
    })
  },


})